package com.jerry.netty.channel;

import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Channel02 {
    public static void main(String[] args) throws Exception {
        String str = "hello,Netty!";

        //创建一个输出流
        FileOutputStream fileOutputStream = new FileOutputStream("file_target.txt");

        //通过fileOutputStream获取对应的FileChannel
        FileChannel fileChannel = fileOutputStream.getChannel();

        //创建一个缓冲区 ByteBuffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(str.length());

        //将Str放入byteBuffer
        byteBuffer.put(str.getBytes());

        //对 byteBuffer 进行 flip
        byteBuffer.flip();

        //将byteBuffer数据写入fileChannel
        fileChannel.write(byteBuffer);

        //关闭输出流
        fileOutputStream.close();
    }
}
